using FytSoa.Common.Enum;
using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using FytSoa.Domain.Hr;
using Mapster;
using Masuit.Tools.Reflection;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp_流程审批记录服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpFlowAuditLogService : IApplicationService 
{
    private readonly SugarRepository<ErpFlowAuditLog> _thisRepository;
    private readonly SugarRepository<HrEmployee> _employeeRepository;
    public ErpFlowAuditLogService(SugarRepository<ErpFlowAuditLog> thisRepository
    ,SugarRepository<HrEmployee> employeeRepository)
    {
        _thisRepository = thisRepository;
        _employeeRepository = employeeRepository;
    }
    
    /// <summary>
    /// 查询审批记录
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpFlowAuditLogDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Status),m=>m.Status==(FlowAuditEnum) int.Parse(param.Status))
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Title.Contains(param.Key))
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        var res=query.Adapt<PageResult<ErpFlowAuditLogDto>>();;
        if (!query.Items.Any()) return res;
        var personIds = query.Items.Select(m => m.PersonId);
        var userList = await _employeeRepository.AsQueryable()
            .Where(m => personIds.Contains(m.Id))
            .Includes(m=>m.OrganizationObj)
            .ToListAsync();
        foreach (var item in res.Items)
        {
            var user = userList.FirstOrDefault(m => m.Id == item.PersonId);
            if(user==null) continue;
            item.PersonName = user.Name;
            item.PersonOrg = user.OrganizationObj.Name;
            item.TypeName = item.Type.GetDescription();
        }
        return res;
    }
    
    /// <summary>
    /// 查询审批记录
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<List<ErpFlowAuditLogDto>> GetLogAsync(ErpFlowUserConfigParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .Where(m=>m.UseId==param.UseId)
            .OrderBy(m=>m.Id)
            .ToListAsync();
        var res=query.Adapt<List<ErpFlowAuditLogDto>>();;
        if (!query.Any()) return res;
        var personIds = query.Select(m => m.PersonId);
        var userList = await _employeeRepository.AsQueryable()
            .Where(m => personIds.Contains(m.Id))
            .Includes(m=>m.OrganizationObj)
            .ToListAsync();
        foreach (var item in res)
        {
            var user = userList.FirstOrDefault(m => m.Id == item.PersonId);
            if(user==null) continue;
            item.PersonName = user.Name;
            item.PersonOrg = user.OrganizationObj.Name;
        }
        return res;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpFlowAuditLogDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        var user = await _employeeRepository.AsQueryable()
            .Where(m => m.Id==model.PersonId)
            .Includes(m=>m.OrganizationObj)
            .FirstAsync();
        var result= model.Adapt<ErpFlowAuditLogDto>();
        result.PersonOrg = user.OrganizationObj.Name;
        result.PersonName = user.Name;
        result.TypeName = model.Type.GetDescription();
        return result;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(ErpFlowAuditLogDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<ErpFlowAuditLog>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(ErpFlowAuditLogDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<ErpFlowAuditLog>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
